package cn.edu.szh.view.baseUI;

import cn.edu.szh.bean.ScoreInfo;
import cn.edu.szh.dao.ScoreDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.List;
import java.util.Vector;

public class ScoreUI extends JFrame {
    JScrollPane scrollPane = new JScrollPane();
    JTextField textFieldStudent_id = new JTextField();//学号
    JTextField textFieldName = new JTextField();//姓名
    JTextField textFieldSubject_id = new JTextField();//课程id
    JTextField textFieldSname = new JTextField();//课程名称
    JTextField textFieldScore = new JTextField();//分数
    JTextField textFieldTerm = new JTextField();//学期
    AbstractTableModel tableModel ;
    String[] columnNames = { "年级id","班级id","学号", "姓名", "课程id", "课程名称", "分数", "学期" };
    ScoreDao scoreDao = new ScoreDao();
    Vector vector= new Vector();
    public ScoreUI()  {
        this.setTitle("成绩信息");
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        JPanel panelUP = new JPanel();
        JPanel panelMid = new JPanel();
        JPanel panelDown = new JPanel();
        add(panelUP, BorderLayout.NORTH);
        add(panelMid, BorderLayout.CENTER);
        add(panelDown, BorderLayout.SOUTH);
        // JScrollPane scrollPane = new JScrollPane();
        panelUP.add(scrollPane);
        createTable();

        panelMid.add(new JLabel("学号:"));
        textFieldStudent_id.setColumns(10);
        panelMid.add(textFieldStudent_id);

        panelMid.add(new JLabel("姓名:"));
        textFieldName.setColumns(10);
        panelMid.add(textFieldName);

        panelMid.add(new JLabel("课程id:"));
        textFieldSubject_id.setColumns(10);
        panelMid.add(textFieldSubject_id);

        panelMid.add(new JLabel("课程名称:"));
        textFieldSname.setColumns(10);
        panelMid.add(textFieldSname);

        panelMid.add(new JLabel("分数:"));
        textFieldScore.setColumns(10);
        panelMid.add(textFieldScore);

        panelMid.add(new JLabel("学期:"));
        textFieldTerm.setColumns(10);
        panelMid.add(textFieldTerm);


        JButton buttonAdd = new JButton("添加");
        panelDown.add(buttonAdd);
        buttonAdd.addActionListener(e -> {
            addScore();
            selectScore();
            reset();
        });

        JButton buttonUpdate = new JButton("修改");
        panelDown.add(buttonUpdate);
        buttonUpdate.addActionListener(e -> {
            updateScore();
            selectScore();
            reset();
        });

        JButton buttonDelete = new JButton("删除");
        panelDown.add(buttonDelete);
        buttonDelete.addActionListener(e -> {
            deleteScore();
            selectScore();
            reset();
        });

        JButton buttonReset = new JButton("重置");
        panelDown.add(buttonReset);
        buttonReset.addActionListener(e -> {
            reset();
            selectScore();
        });

        selectScore();
        setVisible(true);
    }

    private void addScore() {
        ScoreInfo scoreInfo = new ScoreInfo();
        scoreInfo.setStudent_id(Integer.parseInt(textFieldStudent_id.getText()));
        scoreInfo.setName(textFieldName.getText());
        scoreInfo.setSubject_id(Integer.parseInt(textFieldSubject_id.getText()));
        scoreInfo.setSname(textFieldSname.getText());
        scoreInfo.setScore(Double.parseDouble(textFieldScore.getText()));
        scoreInfo.setTerm(textFieldTerm.getText());
        if(ScoreDao.add(scoreInfo)){
            JOptionPane.showMessageDialog(this, "添加成功");
        }else {
            JOptionPane.showMessageDialog(this, "添加失败");
        }
    }

    private void updateScore() {
        ScoreInfo scoreInfo = new ScoreInfo();
        scoreInfo.setStudent_id(Integer.parseInt(textFieldStudent_id.getText()));
        scoreInfo.setName(textFieldName.getText());
        scoreInfo.setSubject_id(Integer.parseInt(textFieldSubject_id.getText()));
        scoreInfo.setSname(textFieldSname.getText());
        scoreInfo.setScore(Double.parseDouble(textFieldScore.getText()));
        scoreInfo.setTerm(textFieldTerm.getText());
        boolean flag = scoreDao.update(scoreInfo);
        if (flag) {
            JOptionPane.showMessageDialog(this, "修改成功");
        }else {
            JOptionPane.showMessageDialog(this, "修改失败");
        }
    }

    private void deleteScore() {
        int student_id = Integer.parseInt(textFieldStudent_id.getText());
        if (scoreDao.delete(student_id)) {
            JOptionPane.showMessageDialog(this, "删除成功");
        }else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }
    }

    private void reset() {
        textFieldStudent_id.setText("");
        textFieldName.setText("");
        textFieldSubject_id.setText("");
        textFieldSname.setText("");
        textFieldScore.setText("");
        textFieldTerm.setText("");
    }

    private void selectScore()  {
        List<ScoreInfo> scoreInfoList = ScoreDao.getAll();
        vector.clear();
        for (ScoreInfo  scoreInfo : scoreInfoList  ) {
            Vector v = new Vector();
            v.add(scoreInfo.getGrade_id());
            v.add(scoreInfo.getClass_id());
            v.add(scoreInfo.getStudent_id());
            v.add(scoreInfo.getName());
            v.add(scoreInfo.getSubject_id());
            v.add(scoreInfo.getSname());
            v.add(scoreInfo.getScore());
            v.add(scoreInfo.getTerm());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }
    private void createTable() {
        JTable table = new JTable(); // 创建表格
        scrollPane.setViewportView(table); // 将表格放入滚动面板中

        // 启用横向滚动条（推荐放在 JScrollPane 初始化时）
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        vector = new Vector();
        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (vector.size() > 0) {
                    Vector v = (Vector) vector.get(rowIndex);
                    return v.get(columnIndex);
                } else {
                    return null;
                }
            }
        };
        table.setModel(tableModel);

        // 设置各列宽度
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            switch (i) {
                case 0:
                    column.setPreferredWidth(70);
                    break;
                case 1:
                    column.setPreferredWidth(70);
                    break;
                case 2:
                    column.setPreferredWidth(70);
                    break;
                case 3:
                    column.setPreferredWidth(70);
                    break;
                case 4:
                    column.setPreferredWidth(70);
                    break;
                case 5:
                    column.setPreferredWidth(70);
                    break;
                case 6:
                    column.setPreferredWidth(70);
                    break;
                case 7:
                    column.setPreferredWidth(70);
                    break;
            }
        }
        // ✅ 禁止自动调整列宽，这样超过窗口宽度时会出现横向滚动条（放到循环外面！）
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }
    public static void main(String[] args) {
        new ScoreUI().setVisible(true);
    }
}
